package models

//User 用户信息表
type User struct {
	ID      int64  `json:"id" xorm:"'id' pk autoincr BIGINT(20)"`
	Name    string `json:"name" xorm:"'name' notnull unique  VARCHAR(64)"`
	Salt    string `json:"salt" xorm:"'salt' VARCHAR(256)"`
	Age     int    `json:"age" xorm:"'age' INT(11)"`
	Passwd  string `json:"passwd" xorm:"'passwd' not null VARCHAR(200)"`
	Created Time   `json:"created" xorm:"'created' TIMESTAMP created"`
	Updated Time   `json:"updated" xorm:"'updated' TIMESTAMP updated"`
	Version int64  `json:"version" xorm:"'version' BIGINT(20) version"`
}
//insert into user(name,passwd) values("admin","123456");
type UserDAO interface {
	GetAllUsers(limit int, start ...int) (users []*User, err error)
	CheckPasswd(user User) (isLogin bool, err error)
	GetUserByID(userID int64)  (user *User,find bool, err error)
	GetUserByName(userName string) (user *User,find bool, err error)
}

//CheckPasswd 校验密码
func (db *dBSession) CheckPasswd(user User) (isLogin bool, err error) {
	_, isLogin, err = db.GetUserByName(user.Name);
	return
}
func (db *dBSession)InsertUser(user User) (int64,error) {
	return db.Insert(user)
}

func (db *dBSession) GetUserByName(userName string) (user *User,find bool, err error) {
	user = new(User)

	if find,err=db.Where("name=?",userName).Get(user); find {
		return user,true, err
	}
	return nil,false, err
}
//GetUserByID 根据用户ID获取用户
func (db *dBSession) GetUserByID(userID int64) (user *User, find bool,err error) {
	user = new(User)
	if find, err = db.ID(userID).Get(user); find {
		return user,true, err
	}
	return nil,false, err
}

//GetAllUsers 获取全部用户
func (db *dBSession) GetAllUsers(limit int, start ...int) (users []*User, err error) {
	//db.NewSession().BeginTrans()
	//err=db.Where("id >?", 0).Limit(10,0).Find(&users)
	err = db.Limit(limit, start...).Find(&users)
	return
}
